import 'dart:async';
import 'dart:typed_data';

import 'src/pasteboard_platform_web.dart'
    if (dart.library.io) 'src/pasteboard_platform_io.dart';

class Pasteboard {
  /// Returns the image data of the pasteboard.
  ///
  /// available on iOS, Desktop, Web, Android
  // static Future<Uint8List?> get image => pasteboard.image;
  static Future<Uint8List?> get image async {
  print('[2222222222 Pasteboard.dart] Getting image from clipboard... 01');
  try {
    final imageData = await pasteboard.image;
    // 记录图片数据长度（避免打印二进制内容）
    print('[2222222222 Pasteboard.dart] Image data received: ${imageData != null ? '${imageData.length} bytes' : 'null'} 02');
    return imageData;
  } catch (e) {
    print('[2222222222 Pasteboard.dart] Failed to get image 03: $e');
    rethrow; // 保持原有错误传播
  }
}

  /// only available on Windows and the web.
  /// Get "HTML format" from system pasteboard.
  /// HTML format: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767917(v=vs.85)
  // static Future<String?> get html => pasteboard.html;
  static Future<String?> get html async {
    print('[2222222222 Pasteboard.dart] Getting HTML from clipboard... 01');
    try {
      final html = await pasteboard.html;
      print('[2222222222 Pasteboard.dart] HTML received: $html 02');
      return html;
    } catch (e) {
      print('[2222222222 Pasteboard.dart] Failed to get HTML 03: $e');
      rethrow; // 保持原有错误传播
    }
  }

  /// available on iOS, Android, Web
  ///
  /// set image data to system pasteboard.
  // static Future<void> writeImage(Uint8List? image) =>
  //     pasteboard.writeImage(image);
  static Future<void> writeImage(Uint8List? image) async {
    print('[2222222222 Pasteboard.dart] Writing image ${image != null ? 'length=${image.length} bytes' : 'null'} 01');
    try {
      await pasteboard.writeImage(image);
      print('[2222222222 Pasteboard.dart] Image write operation completed successfully 02');
    } catch (e) {
      print('[2222222222 Pasteboard.dart] Failed to write image 03: $e');
      rethrow; // 保持原有错误传播
    }
  }

  /// available on Desktop, Android.
  ///
  /// Android: paths are content uris, must read by content resolver.
  ///
  /// Get files from system pasteboard.
  // static Future<List<String>> files() => pasteboard.files();
  static Future<List<String>> files() async {
    print('[2222222222 Pasteboard.dart] Getting files from clipboard... 01');
    try {
      final files = await pasteboard.files();
      print('[2222222222 Pasteboard.dart] Files received: $files 02');
      return files;
    } catch (e) {
      print('[2222222222 Pasteboard.dart] Failed to get files 03: $e');
      rethrow; // 保持原有错误传播
    }
  }

  /// Only available on desktop platforms.
  ///
  /// Set files to system pasteboard.
  // static Future<bool> writeFiles(List<String> files) =>
  //     pasteboard.writeFiles(files);
  static Future<bool> writeFiles(List<String> files) async {
  // 添加日志：记录输入参数和调用开始
  print("[2222222222 Pasteboard.dart] writeFiles called with ${files.length} files 01: $files");
  try {
    final result = await pasteboard.writeFiles(files);
    // 添加日志：记录成功结果
    print("[2222222222 Pasteboard.dart] writeFiles succeeded, result 02: $result");
    return result;
  } catch (e) {
    // 添加日志：记录异常信息
    print("[2222222222 Pasteboard.dart] writeFiles failed 03: $e");
    rethrow; // 保持原有错误传播行为
  }
}

  /// Available on all platforms.
  /// Get text from system pasteboard.
  // static Future<String?> get text => pasteboard.text;
  static Future<String?> get text async {
    print('[2222222222 Pasteboard.dart] Getting text from clipboard... 01');
    try {
      final text = await pasteboard.text;
      print('[2222222222 Pasteboard.dart] Text received: $text 02');
      return text;
    } catch (e) {
      print('[2222222222 Pasteboard.dart] Failed to get text 03: $e');
      rethrow; // 保持原有错误传播
    }
  }

  /// Available on all platforms.
  /// Set text to system pasteboard.
  // static void writeText(String value) => pasteboard.writeText(value);
  static void writeText(String value) async {
    print('[2222222222 Pasteboard.dart] Writing text: $value 01');
    try {
      pasteboard.writeText(value);
      print('[2222222222 Pasteboard.dart] Text write operation completed successfully 02');
    } catch (e) {
      print('[2222222222 Pasteboard.dart] Failed to write text 03: $e');
      rethrow; // 保持原有错误传播
    }
  }
}
